/************************************************************
 * 
 * CCAPI. 2001ff, activestocks.de / Ulrich Staudinger
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html) 
 * 
 ************************************************************/
package org.activequant.core.util;

import java.util.Collections;
import java.util.Comparator;

import org.activequant.core.domainmodel.data.MarketDataEntity;
import org.activequant.core.domainmodel.data.TimeSeries;

/**
 * SortUtils implementation to sort a a list of objects. Objects which should
 * are to be sorted, should implement the Interface sortable. Sortable objects
 * should return int numbers, lower and higher specify the relative position to
 * each other. <br>
 * <b>History:</b><br>
 * - [20.04.2005] Created (Ulrich S.)<br>
 * - [22.04.2006] Added templates/generics etc. => TypeSafety (Erik N.)<br>
 * - [01.10.2007] Simplifying (Ulrich Staudinger)<br>
 * - [27.11.2007] Adding sortByDate and sortById (Ulrich Staudinger)<br>
 * 
 * @author Ulrich Staudinger
 * @author Erik Nijkamp
 */
public class SortUtils {

	/**
	 * sorts a time series by date.<br/>
	 * sorts the MarketDataEntity subclass elements of the given timeSeries(TimeSeries&lt;T&gt;) by their TimeStamp(s)
	 * @param timeSeries
	 */
	public static <T extends MarketDataEntity<T>> void sortByTimeStamp(TimeSeries<T> timeSeries) {
		Comparator<T> c = new Comparator<T>() {
			public int compare(T o1, T o2) {
				return -o1.getTimeStamp().compareTo(o2.getTimeStamp());
			}
		};
		Collections.sort(timeSeries, c);
	}

	/**
	 * sort a time series by id.<br/>
	 * sorts the MarketDataEntity subclass elements of the given timeSeries(TimeSeries&lt;T&gt;) by their id-s(Long)
	 * @param timeSeries
	 */
	public static <T extends MarketDataEntity<T>> void sortById(TimeSeries<T> timeSeries) {
		Comparator<T> c = new Comparator<T>() {
			public int compare(T o1, T o2) {
				return o1.getId() > o2.getId() ? -1 : 1;
			}
		};
		Collections.sort(timeSeries, c);
	}

}
